//
//  ViewController.m
//  GCD常用函数
//
//  Created by 李煜 on 2024/3/20.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//  [self delay];
  [self once];
}
#pragma mark - 一次性代码(整个App运行期)
- (void)once {
  //懒加载也是一次性代码
  //一次性代码不能放到懒加载中
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    NSLog(@"---once");
  });
}

#pragma mark - ///延迟执行
- (void)delay {
  //1. 延迟执行
  //  [self performSelector:@selector(task) withObject:nil afterDelay:2.0];
  //2. 第二种延迟
  //  [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(task) userInfo:nil repeats:YES];

  //3.gcd延迟
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //两秒钟之后在哪中线程执行(根据传递的队列有关)
    [self task];
  });
}

- (void)task {
  NSLog(@"task ----%@",[NSThread currentThread]);
}

@end
